package easy;

/**
 * @author lzy
 * @version v1.0
 * Create DateTime: 2019/2/26 13:14
 */
public class ReverseInteger {
    public static void main(String[] args) {
        Solution solution = new ReverseInteger().new Solution();
        int a = -2147483648;
        System.out.println(solution.reverse(a));
    }

    private class Solution {
        public int reverse(int x) {
            if (x == Integer.MIN_VALUE) {
                return 0;
            }
            int m = x >= 0 ? x : -x;
            String str = String.valueOf(m);
            char[] chars = str.toCharArray();
            for (int i = 0; i < chars.length / 2; i++) {
                int index = chars.length - 1 - i;
                char temp = chars[i];
                chars[i] = chars[index];
                chars[index] = temp;
            }
            String temp = new String(chars);
            long result = Long.parseLong(x > 0 ? temp : "-" + temp);
            ;
            if (result > Integer.MAX_VALUE || result < Integer.MIN_VALUE) {
                return 0;
            }
            return (int) result;
        }
    }
}
